var express = require('express');
var router = express.Router();
var multer = require('multer')
var Storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/userUpload/')
    },
    filename: function (req, file, cb) {
        cb(null, Date.now() + '-' +file.originalname)
    }
})
var upload = multer({storage: Storage});
// var upload = multer({ dest: './public/userUpload/'})
router.post('/', upload.single('avatar'), function (req, res, next) {
    // 提交的字段`avatar`
    // req.body 将具有文本域数据，如果存在的话
    // if (!req.file) {
    //     res.json({ok: false});
    //     return;
    // }
    // 输出文件信息
    console.log('====================================================');
    console.log('fieldname: ' + req.file.fieldname);
    console.log('originalname: ' + req.file.originalname);
    console.log('encoding: ' + req.file.encoding);
    console.log('mimetype: ' + req.file.mimetype);
    console.log('size: ' + (req.file.size / 1024).toFixed(2) + 'KB');
    console.log('destination: ' + req.file.destination);
    console.log('filename: ' + req.file.filename);
    console.log('path: ' + req.file.path);
    res.json({ok:'./userUpload/'+req.file.filename});
    // res.json({ok:req.file.path});

})

router.get('/res', function(req, res, next) {
    res.json({result: 'res'})
});

router.post('/photos/upload', upload.array('photos', 12), function (req, res, next) {
    // req.files 是 `photos` 文件数组的信息
    // req.body 将具有文本域数据，如果存在的话
})
module.exports = router;